import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { usePermissStore } from '../store/permiss';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import Home from '../views/home.vue';
import Login from '@/views/pages/login.vue';
import Register from '@/views/pages/register.vue';
import Platform_admin from '@/views/platform_admin/platform_admin_home.vue'
import Platform_admin_Personnel from '@/views/platform_admin/personnel/personnel.vue'
import Platform_admin_Activity from '@/views/platform_admin/activity/activity.vue'
import Platform_admin_News from '@/views/platform_admin/news/news.vue'
import Platform_admin_University from '@/views/platform_admin/university/university.vue'
import University_admin from '@/views/university_admin/university_admin_home.vue';
import University_admin_Activity from '@/views/university_admin/activity.vue'
import University_admin_News from '@/views/university_admin/news.vue'
import University_admin_Publicity from '@/views/university_admin/publicity.vue'
import University_admin_EnrollmentNotice from '@/views/university_admin/enrollment_notice.vue'
import University_admin_YearsEnrollment from '@/views/university_admin/years_enrollment.vue'
import University_admin_Major from '@/views/university_admin/major.vue'
import University_admin_UserCenter from '@/views/university_admin/user_center.vue'

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/login',
    },
    {//平台管理员登录界面
        path:'/platform_admin',
        name: 'platform-admin',
        component: Platform_admin,
        children:[
            {
                path: '/platform_admin_personnel',
                name: 'personnel',
                meta: {
                    title: '人员管理',
                    permiss: '1-2',
                },
                component: Platform_admin_Personnel,
            },
            {
                path: '/platform_admin_activity',
                name: 'activity',
                meta: {
                    title: '活动管理',
                    permiss: '1-3',
                },
                component: Platform_admin_Activity,
            },
            {
                path: '/platform_admin_news',
                name: 'news',
                meta: {
                    title: '新闻管理',
                    permiss: '1-5',
                },
                component: Platform_admin_News,
            },
            {
                path: '/platform_admin_university',
                name: 'university',
                meta: {
                    title: '高校管理',
                    permiss: '1-6',
                },
                component: Platform_admin_University,
            },
        ]
    },
    {//高校管理员登录界面
        path:'/university_admin',
        name: 'university-admin',
        component: University_admin,
        children:[
            {
                path: '/university_admin_activity',
                name: 'university_admin_activity',
                meta: {
                    title: '活动管理',
                    permiss: '2-1',
                },
                component: University_admin_Activity,
            },
            {
                path: '/university_admin_news',
                name: 'university_admin_news',
                meta: {
                    title: '新闻管理',
                    permiss: '2-2',
                },
                component: University_admin_News,
            },
            {
                path: '/university_admin_publicity',
                name: 'publicity',
                meta: {
                    title: '校园宣传管理',
                    permiss: '2-3',
                },
                component: University_admin_Publicity,
            },
            {
                path: '/university_admin_years_enrollment',
                name: 'years_enrollment',
                meta: {
                    title: '历年招生管理',
                    permiss: '2-4',
                },
                component: University_admin_YearsEnrollment,
            },
            {
                path: '/university_admin_enrollment_notice',
                name: 'enrollment_notice',
                meta: {
                    title: '招生公告管理',
                    permiss: '2-5',
                },
                component: University_admin_EnrollmentNotice,
            },
            {
                path: '/university_admin_major',
                name: 'major',
                meta: {
                    title: '专业信息管理',
                    permiss: '2-6',
                },
                component: University_admin_Major,
            },
            {
                path: '/university_admin_user_center',
                name: 'user_center',
                meta: {
                    title: '个人中心',
                    permiss: '2-7',
                },
                component: University_admin_UserCenter,
            },
        ]
    },
    // {
    //     path: '/',
    //     name: 'Home',
    //     component: Home,
    //     children: [
    //         {
    //             path: '/dashboard',
    //             name: 'dashboard',
    //             meta: {
    //                 title: '系统首页',
    //                 permiss: '0',
    //             },
    //             component: () => import(/* webpackChunkName: "dashboard" */ '../views/dashboard.vue'),
    //         },
    //         {
    //             path: '/system-user',
    //             name: 'system-user',
    //             meta: {
    //                 title: '用户管理',
    //                 permiss: '1-1-1',
    //             },
    //             component: () => import(/* webpackChunkName: "system-user" */ '../views/system/user.vue'),
    //         },
    //         {
    //             path: '/system-role',
    //             name: 'system-role',
    //             meta: {
    //                 title: '角色管理',
    //                 permiss: '1-1-2',
    //             },
    //             component: () => import(/* webpackChunkName: "system-role" */ '../views/system/role.vue'),
    //         },
    //         {
    //             path: '/system-menu',
    //             name: 'system-menu',
    //             meta: {
    //                 title: '菜单管理',
    //                 permiss: '1-1-3',
    //             },
    //             component: () => import(/* webpackChunkName: "system-menu" */ '../views/system/menu.vue'),
    //         },
    //         {
    //             path: '/personnel',
    //             name: 'personnel',
    //             meta: {
    //                 title: '人员管理',
    //                 permiss: '1-2',
    //             },
    //             component: () => import(/* webpackChunkName: "system-permission" */ '../views/personnel/personnel.vue'),
    //         },
    //         {
    //             path: '/activity',
    //             name: 'activity',
    //             meta: {
    //                 title: '活动管理',
    //                 permiss: '1-3',
    //             },
    //             component: () => import(/* webpackChunkName: "system-permission" */ '../views/activity/activity.vue'),
    //         },
    //         {
    //             path: '/notice',
    //             name: 'notice',
    //             meta: {
    //                 title: '公告管理',
    //                 permiss: '1-4',
    //             },
    //             component: () => import(/* webpackChunkName: "system-permission" */ '../views/notice/notice.vue'),
    //         },
    //         {
    //             path: '/news',
    //             name: 'news',
    //             meta: {
    //                 title: '新闻管理',
    //                 permiss: '1-5',
    //             },
    //             component: () => import(/* webpackChunkName: "system-permission" */ '../views/news/news.vue'),
    //         },
    //         {
    //             path: '/university',
    //             name: 'university',
    //             meta: {
    //                 title: '高校管理',
    //                 permiss: '1-6',
    //             },
    //             component: () => import(/* webpackChunkName: "system-permission" */ '../views/university/university.vue'),
    //         },
    //         {
    //             path: '/table',
    //             name: 'basetable',
    //             meta: {
    //                 title: '基础表格',
    //                 permiss: '31',
    //             },
    //             component: () => import(/* webpackChunkName: "table" */ '../views/table/basetable.vue'),
    //         },
    //         {
    //             path: '/table-editor',
    //             name: 'table-editor',
    //             meta: {
    //                 title: '可编辑表格',
    //                 permiss: '32',
    //             },
    //             component: () => import(/* webpackChunkName: "table-editor" */ '../views/table/table-editor.vue'),
    //         },
    //         {
    //             path: '/schart',
    //             name: 'schart',
    //             meta: {
    //                 title: 'schart图表',
    //                 permiss: '41',
    //             },
    //             component: () => import(/* webpackChunkName: "schart" */ '../views/chart/schart.vue'),
    //         },
    //         {
    //             path: '/echarts',
    //             name: 'echarts',
    //             meta: {
    //                 title: 'echarts图表',
    //                 permiss: '42',
    //             },
    //             component: () => import(/* webpackChunkName: "echarts" */ '../views/chart/echarts.vue'),
    //         },

    //         {
    //             path: '/icon',
    //             name: 'icon',
    //             meta: {
    //                 title: '图标',
    //                 permiss: '5',
    //             },
    //             component: () => import(/* webpackChunkName: "icon" */ '../views/pages/icon.vue'),
    //         },
    //         {
    //             path: '/ucenter',
    //             name: 'ucenter',
    //             meta: {
    //                 title: '个人中心',
    //             },
    //             component: () => import(/* webpackChunkName: "ucenter" */ '../views/pages/ucenter.vue'),
    //         },
    //         {
    //             path: '/editor',
    //             name: 'editor',
    //             meta: {
    //                 title: '富文本编辑器',
    //                 permiss: '291',
    //             },
    //             component: () => import(/* webpackChunkName: "editor" */ '../views/pages/editor.vue'),
    //         },
    //         {
    //             path: '/markdown',
    //             name: 'markdown',
    //             meta: {
    //                 title: 'markdown编辑器',
    //                 permiss: '292',
    //             },
    //             component: () => import(/* webpackChunkName: "markdown" */ '../views/pages/markdown.vue'),
    //         },
    //         {
    //             path: '/export',
    //             name: 'export',
    //             meta: {
    //                 title: '导出Excel',
    //                 permiss: '34',
    //             },
    //             component: () => import(/* webpackChunkName: "export" */ '../views/table/export.vue'),
    //         },
    //         {
    //             path: '/import',
    //             name: 'import',
    //             meta: {
    //                 title: '导入Excel',
    //                 permiss: '33',
    //             },
    //             component: () => import(/* webpackChunkName: "import" */ '../views/table/import.vue'),
    //         },
    //         {
    //             path: '/theme',
    //             name: 'theme',
    //             meta: {
    //                 title: '主题设置',
    //                 permiss: '7',
    //             },
    //             component: () => import(/* webpackChunkName: "theme" */ '../views/pages/theme.vue'),
    //         },
    //         {
    //             path: '/calendar',
    //             name: 'calendar',
    //             meta: {
    //                 title: '日历',
    //                 permiss: '24',
    //             },
    //             component: () => import(/* webpackChunkName: "calendar" */ '../views/element/calendar.vue'),
    //         },
    //         {
    //             path: '/watermark',
    //             name: 'watermark',
    //             meta: {
    //                 title: '水印',
    //                 permiss: '25',
    //             },
    //             component: () => import(/* webpackChunkName: "watermark" */ '../views/element/watermark.vue'),
    //         },
    //         {
    //             path: '/carousel',
    //             name: 'carousel',
    //             meta: {
    //                 title: '走马灯',
    //                 permiss: '23',
    //             },
    //             component: () => import(/* webpackChunkName: "carousel" */ '../views/element/carousel.vue'),
    //         },
    //         {
    //             path: '/tour',
    //             name: 'tour',
    //             meta: {
    //                 title: '分步引导',
    //                 permiss: '26',
    //             },
    //             component: () => import(/* webpackChunkName: "tour" */ '../views/element/tour.vue'),
    //         },
    //         {
    //             path: '/steps',
    //             name: 'steps',
    //             meta: {
    //                 title: '步骤条',
    //                 permiss: '27',
    //             },
    //             component: () => import(/* webpackChunkName: "steps" */ '../views/element/steps.vue'),
    //         },
    //         {
    //             path: '/form',
    //             name: 'forms',
    //             meta: {
    //                 title: '表单',
    //                 permiss: '21',
    //             },
    //             component: () => import(/* webpackChunkName: "form" */ '../views/element/form.vue'),
    //         },
    //         {
    //             path: '/upload',
    //             name: 'upload',
    //             meta: {
    //                 title: '上传',
    //                 permiss: '22',
    //             },
    //             component: () => import(/* webpackChunkName: "upload" */ '../views/element/upload.vue'),
    //         },
    //         {
    //             path: '/statistic',
    //             name: 'statistic',
    //             meta: {
    //                 title: '统计',
    //                 permiss: '28',
    //             },
    //             component: () => import(/* webpackChunkName: "statistic" */ '../views/element/statistic.vue'),
    //         },
    //     ],
    // },
    {
        path: '/login',
        meta: {
            title: '登录',
            noAuth: true,
        },
        component: Login,
    },
    {
        path: '/register',
        meta: {
            title: '注册',
            noAuth: true,
        },
        component: Register,
    },
    {
        path: '/reset-pwd',
        meta: {
            title: '重置密码',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "reset-pwd" */ '../views/pages/reset-pwd.vue'),
    },
    {
        path: '/403',
        meta: {
            title: '没有权限',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "403" */ '../views/pages/403.vue'),
    },
    {
        path: '/404',
        meta: {
            title: '找不到页面',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "404" */ '../views/pages/404.vue'),
    },
    // { path: '/:path(.*)', redirect: '/404' },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    // 显示进度条
    NProgress.start();
    const token = localStorage.getItem('token');
    const permiss = usePermissStore();

    if (!token && to.meta.noAuth !== true) {
        next('/login');
    } 
    // else if (typeof to.meta.permiss == 'string' && !permiss.key.includes(to.meta.permiss)) {
    //     // 如果没有权限，则进入403
    //     next('/403');
    // } 
    else {
        next();
    }
});

router.afterEach(() => {
    NProgress.done();
});

export default router;
